﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ShoozlaWebService;
using System.ServiceModel.Channels;

namespace ShoozlaWCFService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class UsageStatistics : IUsageStatistics
    {
        private DBHelperInstance db = DBHelperInstance.GetInstance();
        public void SoftwareStarted()
        {
            //add some statistics to a database, "best effort" --> ignore errors
            try
            {
                DateTime time = DateTime.Now;
                String ip;

                //getting client ip
                OperationContext context = OperationContext.Current;
                MessageProperties prop = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                ip = endpoint.Address;

                string addStatistics = "INSERT INTO TB_BUDDYNGER_STAT (IP,DATE) ";
                addStatistics += "VALUES ('" + ip + "','" + time.ToShortDateString() + "')";
                db.NonQueryToDB(addStatistics);

            }
            catch
            {
                return; //error - return empty 
            }
        }
    }
 
}
